/*
Program: DC-SAR, File sql.c
Copyright (C) 2009 Davy De Coninck

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "conf.h"
#include "sql.h"
#include <mysql.h>
#include <stdio.h>
#include <string.h>

MYSQL *mconn;

void openconnection(clsconfig *conf)
{
	mconn = mysql_init(NULL);

	/* Connect to database */
	mysql_real_connect(mconn, conf->dbhost,
		conf->dbuser, conf->dbpass, conf->dbname, 0, NULL, 0);
}

void closeconnection()
{
	mysql_close(mconn);
}

int connectiontest(clsconfig *conf)
{
	   MYSQL *conn;
	   MYSQL_RES *res;
	   MYSQL_ROW row;

	   conn = mysql_init(NULL);

	   /* Connect to database */
	   if (!mysql_real_connect(conn, conf->dbhost,
		 conf->dbuser, conf->dbpass, conf->dbname, 0, NULL, 0)) {
	      fprintf(stderr, "%s\n", mysql_error(conn));
	      return 1;
	   }

	   /* send SQL query */
	   if (mysql_query(conn, "show tables")) {
	      fprintf(stderr, "%s\n", mysql_error(conn));
	      return 1;
	   }

	   res = mysql_use_result(conn);
		mysql_free_result(res);

	   //testen van de Const Tabel
	   if (mysql_query(conn, "CREATE TABLE IF NOT EXISTS `Const` (`ConstId` int(11) NOT NULL auto_increment,`Const` varchar(100) NOT NULL, `Value` varchar(100) NOT NULL, PRIMARY KEY  (`ConstId`), KEY `Const` (`Const`)) ENGINE=MyISAM")){
		fprintf(stderr, "%s\n", mysql_error(conn));
	      	return 1;
	}

	   //testen van de logbook
	   if (mysql_query(conn, "create table IF NOT EXISTS logbook(logbookid int auto_increment NOT NULL, Timestamp datetime NOT NULL, Elapsed bigint NOT NULL, Client varchar(255) NOT NULL, Action_code varchar(1000) NOT NULL, Size bigint NOT NULL, Method varchar(255) NOT NULL, URI varchar(10000) NOT NULL, Ident varchar(255) NOT NULL, Hierarchy varchar(255) NOT NULL, Content varchar(255) NOT NULL, PRIMARY KEY  (`logbookid`));")){

		fprintf(stderr, "%s\n", mysql_error(conn));
		return 1;
	}

	   /* close connection */
	   //mysql_free_result(res);
	   mysql_close(conn);
	
	   return 0;
}

char *Get_Value(char *name)
{
	char query[512];
	char *return_result;
	MYSQL_ROW row;
	MYSQL_RES *result;
	
	snprintf(query, sizeof(query), "SELECT Value FROM Const WHERE Const='%s'", name);
	Get_Query(query);

	if ((result = mysql_use_result(mconn)) == NULL){
		return "";
	}
	
	if ((row = mysql_fetch_row(result)) == NULL){
		snprintf(query, sizeof(query), "INSERT into Const (Const, Value) VALUES ('%s','')", name);
		Get_Query(query);
		return "";
	}

	asprintf(&return_result, "%s", row[0]);
	mysql_free_result(result);
	
	return return_result;
}

char *Update_Value(char *name, char *newval)
{
	char query[512];
	char *return_result;
	MYSQL_ROW row;
	MYSQL_RES *result;
	
	snprintf(query, sizeof(query), "Update Const set Value = '%s' WHERE Const='%s'", newval, name);
	Get_Query(query);

	if ((result = mysql_use_result(mconn)) == NULL){
		return "";
	}
	
	if ((row = mysql_fetch_row(result)) == NULL){
		snprintf(query, sizeof(query), "INSERT into Const (Const, Value) VALUES ('%s','%s')", name, newval);
		Get_Query(query);
		return "";
	}

	asprintf(&return_result, "%s", row[0]);
	mysql_free_result(result);
	
	return return_result;
}

int Get_Query(char *query)
{
	int ret;
	if ((ret = mysql_real_query(mconn, query, (unsigned int) strlen(query))) != 0)
		printf("ERROR: %s", mysql_error(mconn));
	
	return 0;
}
